package org.huangrui.spark.scala.core.acc

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author hr
 * @Create 2024-10-19 20:27
 */
object Spark01_Acc {
  def main(args: Array[String]): Unit = {
    val sparConf = new SparkConf().setMaster("local").setAppName("WordCount")
    val sc = new SparkContext(sparConf)
    val rdd = sc.makeRDD(List(1, 2, 3, 4))
    // reduce : 分区内计算，分区间计算
    val i: Int = rdd.reduce(_ + _)
    println(i)
    //  RDD在foreach循环时，逻辑代码和操作全部都是在Executor端完成的，那么结果不会拉取回到Driver端
    var sum = 0
    rdd.foreach(
      num => sum += num
    )

    println("sum = " + sum)
    sc.stop()
  }
}
